/**
 * 
 */
package com.couchcrowler.shared;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * @author vienozin
 *
 */
public class ForgetfulSet<E> extends HashSet<E> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6011150345659623637L;

	private final long timeout;
	
	private final Map<E, Long> timeMap = new HashMap<E, Long>();
	
	public ForgetfulSet(long timeout) {
		this.timeout = timeout;
	}

	@Override
	public boolean contains(Object element) {
		Long time = timeMap.get(element);
		if (time != null && System.currentTimeMillis() - time > timeout) {
			remove(element);
			timeMap.remove(element);
		}
		return super.contains(element);
	}

	@Override
	public boolean add(E element) {
		timeMap.put(element, System.currentTimeMillis());
		return super.add(element);
	}	
}
